package exhi.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stp.core.entity.IGridRowFactory;
import com.stp.core.entity.XGrid;
import com.stp.core.entity.XGridPage;
import com.stp.core.entity.XGridRow;
import com.stp.core.util.Identities;
import com.stp.survey.entity.SurveyInfo;
import com.stp.survey.manager.SurveyMng;

import exhi.entity.EntpAsse;
import exhi.entity.EntpBexhi;
import exhi.entity.EntpUser;
import exhi.manager.EntpAsseMng;
import exhi.manager.EntpBexhiMng;

@Controller
@RequestMapping("/entp")
public class EntpExhiAct extends BaseAct {

	private static final String PATH_TABLE = "/exhi-table";
	private static final String PATH_LIST = "/exhi-list";
	private static final String PATH_VIEW = "/exhi-view";
	private static final String PATH_SAVE = "/exhi-save";

	private String code = "";
	private static final String PATH_SURVEY_VIEW = "/survey-view";
	// 打印大型展
	private static final String PRINT_BEXHI = "/print-bexhi";

	// 详细信息
	@RequestMapping(PATH_SURVEY_VIEW)
	public void surveyView(@RequestParam("code") String code,
			@RequestParam("newCode") String newCode,
			@ModelAttribute("surveyInfo") SurveyInfo surveyInfo,
			@ModelAttribute("entpBexhi") EntpBexhi entpBexhiInfo, ModelMap model) {
		model.addAttribute("colName", "展后评估");
		model.addAttribute("sid", surveyInfo.getId());
		if (null == surveyInfo.getId()
				|| "".equalsIgnoreCase(surveyInfo.getId())) {
			model.addAttribute("surveyId", Identities.uuid());
		} else {
			model.addAttribute("surveyId", surveyInfo.getId());
		}
		model.addAttribute("code", code);
		model.addAttribute("newCode", newCode);
		model.addAttribute("survey", surveyInfo);
		model.addAttribute("info", entpBexhiInfo);
		model.addAttribute("quests",
				surveyMng.findQuest("surveyId", surveyInfo.getId(), "sort"));
	}

	// 打印大型展
	@RequestMapping(PRINT_BEXHI)
	public void printBexhi(@ModelAttribute("entpBexhi") EntpBexhi entpBexhi,
			ModelMap model) {
		showView(entpBexhi, model);
	}

	@ModelAttribute("surveyInfo")
	public SurveyInfo getSurveyInfo(
			@RequestParam(value = "sid", required = true, defaultValue = "") String code) {
		SurveyInfo surveyInfo = surveyMng.get(code);
		if (surveyInfo == null)
			surveyInfo = new SurveyInfo();
		return surveyInfo;
	}

	@RequestMapping(PATH_TABLE)
	public void table(@RequestParam(value = "code") String code, ModelMap model) {
		model.addAttribute("code", code);
		if ("20".equalsIgnoreCase(code)) {
			model.addAttribute("colName", "展览会审核");
		}

		if ("30".equalsIgnoreCase(code)) {
			model.addAttribute("colName", "合同确认");
		}

		if ("50".equalsIgnoreCase(code)) {
			model.addAttribute("colName", "打印报批件");
		}

		if ("60".equalsIgnoreCase(code)) {
			model.addAttribute("colName", "展览会评估");
		}

		if ("90".equalsIgnoreCase(code)) {
			model.addAttribute("colName", "展览会查询");
		}
	}

	// 权限json数据
	@RequestMapping(PATH_LIST)
	@ResponseBody
	public String infoList(XGridPage page, @RequestParam("code") String code,
			@RequestParam("cap_code") String capCode,
			@RequestParam("cap_name") String capName)
			throws InstantiationException, IllegalAccessException {
		this.code = code;
		// String sql = "from EntpBexhi info where info.status in ('" + code+
		// "') ";
		String sql = "from EntpBexhi info where 1=1";
		sql += sqlModel.getSQLChars("info.zhlx", "=", capCode, false, "and");
		sql += sqlModel
				.getSQLChars("info.zlhmc", "like", capName, false, "and");
		sql += " order by info.createtime desc";
		ExhiGridRow row = new EntpExhiAct.ExhiGridRow();
		XGrid grid = entpBexhiMng.getXGrid(sql, row, page.getPageNo(),
				page.getPageSize(), false);
		if (null == grid)
			return "";
		return "{\"xGrid\":" + toJson(grid) + "}";
	}

	class ExhiGridRow implements IGridRowFactory {
		public XGridRow builderXRow(Object obj) {
			// TODO Auto-generated method stub
			XGridRow xrow = null;
			String s = "";
			String c1 = "";
			String c0 = "";

			if (obj instanceof EntpBexhi) {
				xrow = new XGridRow();
				EntpBexhi info = (EntpBexhi) obj;
				xrow.setId(info.getId());
				c0 = info.getStatus();
				List t = new ArrayList();
				t.add("");
				t.add(info.getZlhmc());
				t.add(getUrl(info.getId()));
				xrow.setData(t);
			}
			return xrow;
		}
	}

	private String getUrl(String id) {
		String s = "";
		if ("60".equalsIgnoreCase(code)) {
			String surveyId = "";
			String asseId = "";
			surveyId = getSurveyId("c2e");
			asseId = getAsseId(id);
			EntpUser u = getEntpUser();
			if (null != u) {
				if (u.getUserType() != null && "Z".equalsIgnoreCase(u.getUserType() )) {
					// 企业评估场馆
					s = "<a href='../gov/exhi-survey-detail.jspx?sid=" + surveyId + "&type=c2e&infoId=" + id
							+ "'>【对场馆评测】</a>";
					/*
					s += "  <a href='asse-detail.jspx?code=B&infoId=" + id
							+ "'>【展前预估】</a>";
				   */
					s += "  <a href='asse-detail.jspx?code=A&infoId=" + id
							+ "'>【展后自评】</a>";
				}
				if (u.getUserType() != null && "E".equalsIgnoreCase(u.getUserType() )) {
					surveyId = getSurveyId("e2c");
					// 场馆评估企业
					s += "  <a href='../gov/exhi-survey-detail.jspx?sid=" + surveyId + "&type=e2c&infoId=" + id
							+ "'>【对企业评测】</a>";
				}
			}			
		} else {
			s = "<a href='print-bexhi.jspx?infoId=" + id
					+ "' target='_blank'>【打印批件】</a>";
		}
		return s;
	}

	@RequestMapping(PATH_VIEW)
	public void infoView(@RequestParam("code") String code,
			@RequestParam("newCode") String newCode,
			@ModelAttribute("entpBexhi") EntpBexhi entpBexhiInfo, ModelMap model) {
		model.addAttribute("code", code);
		model.addAttribute("newCode", newCode);
		model.addAttribute("info", entpBexhiInfo);
		showView(entpBexhiInfo, model);
	}

	private void showView(EntpBexhi entpBexhi, ModelMap model) {
		model.addAttribute("info", entpBexhi);
		String zb = "";
		String qt = "";
		String cb = "";
		String zc = "";
		String xb = "";
		String exhiType = "";
		String zldd = "";

		if (entpBexhi.getId() == null) {
			// 第一主办方
			model.addAttribute("entpName", getEntpUser().getMc());
			model.addAttribute("projectId", Identities.uuid());
		} else {
			// 第一主办方
			// model.addAttribute("entpName", entpMng.get(entpBexhi.getUid())
			// .getMc());
			model.addAttribute("projectId", entpBexhi.getId());
			zb = this.getEntpRelaction(entpBexhi.getId(), "ZB");// 主办单位
			qt = this.getEntpRelaction(entpBexhi.getId(), "QT");// 其他单位
			cb = this.getEntpRelaction(entpBexhi.getId(), "CB");// 承办单位
			zc = this.getEntpRelaction(entpBexhi.getId(), "ZC");// 支持单位
			xb = this.getEntpRelaction(entpBexhi.getId(), "XB");// 协办单位

			exhiType = this.getEntpRelaction(entpBexhi.getId(), "exhiType");// 展会类型
			zldd = this.getEntpRelaction(entpBexhi.getId(), "zldd");// 展会场馆

		}
		model.addAttribute("zb", zb);
		model.addAttribute("qt", qt);
		model.addAttribute("cb", cb);
		model.addAttribute("zc", zc);
		model.addAttribute("xb", xb);
		model.addAttribute("exhiType", exhiType);
		model.addAttribute("zldd", zldd);
	}

	// 数据保存
	@RequestMapping(PATH_SAVE)
	public String saveInfo(@RequestParam("code") String code,
			@RequestParam("newCode") String newCode,
			@ModelAttribute("entpBexhi") EntpBexhi entpBexhiInfo, ModelMap model)
			throws IllegalStateException, IOException {
		entpBexhiInfo.setCreatetime(new Date());
		if (code.equalsIgnoreCase(entpBexhiInfo.getStatus())) {
			entpBexhiInfo.setStatus(newCode);
		}
		entpBexhiMng.save(entpBexhiInfo);
		return "redirect:" + PATH_TABLE.substring(1) + ".jspx?code=" + code;
	}

	protected String getAsseId(String id) {
		String sql = "from EntpAsse where uid='" + id + "'";
		List<EntpAsse> lst = entpAsseMng.list(sql);
		String sid = "";
		for (EntpAsse s : lst)
			sid = s.getId();
		return sid;
	}

	@ModelAttribute("entpBexhi")
	public EntpBexhi getEntpSexhiInfo(
			@RequestParam(value = "infoId", required = true, defaultValue = "") String id) {
		EntpBexhi entpBexhi = entpBexhiMng.get(id);
		if (null == entpBexhi) {
			entpBexhi = new EntpBexhi();
		}
		return entpBexhi;
	}

	@Autowired
	@Qualifier("entpBexhiMng")
	private EntpBexhiMng entpBexhiMng;

	@Autowired
	@Qualifier("surveyMng")
	private SurveyMng surveyMng;

	@Autowired
	@Qualifier("entpAsseMng")
	private EntpAsseMng entpAsseMng;

}
